package com.xiangty.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.xiangty.bean.UserContact;

public interface UserContactMapper {

	/**
	 * 查询userContact信息，方法一：mapUnderscoreCamelCase驼峰命名配置
	 * 
	 * @return
	 */
	@Select({ "SELECT id, email, phone, home_address, user_id FROM user_contact" })
	List<UserContact> selectUserContact();

	/**
	 * 查询userContact信息，方法二：使用resultMap方式
	 * 
	 * @return
	 */
	// @Results 中id属性设置id属性后其他方法可以通过id属性引用
	@Results(id = "userContactResultMap", value = {
			// @Result注解id=true表示对应<id>元素
			@Result(property = "id", column = "id", id = true), @Result(property = "email", column = "email"),
			@Result(property = "phone", column = "phone"),
			@Result(property = "homeAddress", column = "home_address"), })
	@Select({ "SELECT id, email, phone, home_address FROM user_contact" })
	List<UserContact> selectUserContact2();

	/**
	 * 效果和selectUserContact2
	 * 
	 * @return
	 */
	@ResultMap("userContactResultMap")
	@Select({ "SELECT id, email, phone, home_address FROM user_contact" })
	List<UserContact> selectUserContact3();

	/**
	 * 不需要返回主键
	 * 
	 * @param userContact
	 * @return
	 */
	@Insert({ "insert into user_contact(email, phone, home_address, user_id) values (#{email}, #{phone}, #{homeAddress}, #{user.id})" })
	int insert1(UserContact userContact);

	/**
	 * 返回自增主键
	 * 
	 * @param userContact
	 * @return
	 */
	@Insert({ "insert into user_contact(email, phone, home_address, user_id) values (#{email}, #{phone}, #{homeAddress}, #{user.id})" })
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	int insert2(UserContact userContact);
	
	/**
	 * 返回非自增主键
	 * 
	 * @param userContact
	 * @return
	 */
	@Insert({ "insert into user_contact(email, phone, home_address, user_id) values (#{email}, #{phone}, #{homeAddress}, #{user.id})" })
	@SelectKey(statement="SELECT LAST_INSERT_ID()",
				keyProperty = "id", 
				keyColumn = "id",
				resultType = Integer.class,
				before = false)
	int insert3(UserContact userContact);

	/**
	 * 更新数据
	 * @param userContact
	 * @return
	 */
	@Update({"update user_contact set email=#{email}, phone=#{phone}, home_address=#{homeAddress}, user_id=#{user.id} where id=#{id}"})
	int updateById(UserContact userContact);
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@Delete({"delete from user_contact where id = #{id}"})
	int deleteById(Integer id);
	
}
